              package com.java.diagnostics.visualizer.impl.marshalling;
              
              import com.java.diagnostics.visualizer.exceptions.GCAndMemoryVisualizerParsedDataCorruptedException;
              import com.java.diagnostics.visualizer.impl.GCAndMemoryVisualizerImplPreferenceInitalizer;
              import com.java.diagnostics.visualizer.impl.factory.LogFactory;
              import com.java.diagnostics.visualizer.impl.preferences.GCAndMemoryVisualizerPreferenceInitializer;
              import com.java.diagnostics.visualizer.impl.preferences.SmartPreferences;
              import com.java.diagnostics.visualizer.impl.templates.TemplateList;
              import com.java.diagnostics.visualizer.impl.util.Messages;
              import com.java.diagnostics.visualizer.properties.OutputProperties;
              import java.io.File;
              import java.io.InputStream;
              import java.text.MessageFormat;
              import java.util.logging.Logger;
              
              public abstract class AbstractArgumentHandler
              {
                private static final String PREFERENCES_FILE_DOES_NOT_EXIST = Messages.getString("ArgumentHandler.preferences.do.not.exist");
              
                private static final String FILE_DOES_NOT_EXIST = Messages.getString("ArgumentHandler.file.does.not.exist");
                private static final String DASH = "-";
                private static final String P = "-p";
                private static final String O = "-o";
                private static final String D = "-d";
                private static final String F = "-f";
                private static final String H = "-h";
                private static final String RC = "-rc";
                private static final String DISPLAY = "-display";
                private static final String HELP = "-help";
                private String[] strings;
                protected OutputProperties outputProperties;
                protected boolean setReturnCodeOnError = false;
              
                private static final String HELP_TEXT = Messages.getString("ArgumentHandler.1") + 
                  Messages.getString("ArgumentHandler.2") + 
                  Messages.getString("ArgumentHandler.3") + 
                  Messages.getString("ArgumentHandler.4") + 
                  Messages.getString("ArgumentHandler.17") + 
                  Messages.getString("ArgumentHandler.18") + 
                  Messages.getString("ArgumentHandler.19") + 
                  Messages.getString("ArgumentHandler.35") + 
                  Messages.getString("ArgumentHandler.24") + 
                  Messages.getString("ArgumentHandler.25");
              
                private static final String EXAMPLE_TEXT = Messages.getString("ArgumentHandler.31");
              
                private static final Logger TRACE = LogFactory.getTrace(AbstractArgumentHandler.class);
                protected static final String EMPTY_STRING = "";
              
                public AbstractArgumentHandler(Object args)
                {
                  if ((args instanceof String[]))
                    this.strings = ((String[])((String[])args).clone());
                  else
                    this.strings = new String[0];
                }
              
                public boolean initialise(OutputProperties props)
                {
                  this.outputProperties = props;
                  boolean hitFileArguments = false;
              
                  for (int i = 0; i < this.strings.length; i++) {
                    String arg = this.strings[i];
                    if (("-help".equals(arg)) || ("-h".equals(arg))) {
                      printHelp();
                      return false;
                    }if ("-f".equals(arg)) {
                      String nextArg = null;
                      while ((i < this.strings.length - 1) && (!isOption(nextArg))) {
                        i++;
                        nextArg = this.strings[i];
                        if (!isOption(nextArg)) {
                          hitFileArguments = true;
              
                          File whole = new File(nextArg);
                          if (whole.exists()) {
                            try {
                              addFile(whole);
                            } catch (GCAndMemoryVisualizerParsedDataCorruptedException e) {
                              TRACE.warning(MessageFormat.format(
                                Messages.getString("ArgumentHandler.32"), 
                                new Object[] { whole.getAbsolutePath(), 
                                e }));
                            }
                          }
                          else
                          {
                            String[] arrayOfString1;
                            int j = (arrayOfString1 = nextArg.split(File.pathSeparator)).length;
                            
                         for (int i1 = 0; i1 < j; i1++) {
                              String fileName = arrayOfString1[i1];
                              File file = new File(fileName);
                              if (!file.exists()) {
                                TRACE.warning(MessageFormat.format(
                                  FILE_DOES_NOT_EXIST, 
                                  new Object[] { file
                                  .getAbsolutePath() }));
                                return false;
                              }
                              try {
                                addFile(file);
                              } catch (GCAndMemoryVisualizerParsedDataCorruptedException e) {
                                TRACE
                                  .warning(
                                  MessageFormat.format(
                                  Messages.getString("ArgumentHandler.32"), 
                                  new Object[] { 
                                  file
                                  .getAbsolutePath(), 
                                  e }));
                              }
              
                            }
              
                          }
              
                        }
              
                      }
              
                      if (!hitFileArguments) {
                        printHelp();
                        return false;
                      }
              
                      i--;
                    }
                    else if ("-d".equals(arg))
                    {
                      i++;
                      if (i < this.strings.length) {
                        String nextArg = this.strings[i];
                        if (isOption(nextArg)) {
                          printHelp();
                          return false;
                        }
                        File file = new File(nextArg);
                        addDirectory(file);
                        hitFileArguments = true;
                      }
                    }
                    else if ("-rc".equals(arg)) {
                      this.setReturnCodeOnError = true;
                    }
                    else if ("-o".equals(arg)) {
                      i++;
                      if (i < this.strings.length) {
                        String nextArg = this.strings[i];
                        if (isOption(nextArg)) {
                          printHelp();
                          return false;
                        }
                        setOutputFolder(nextArg);
                      }
              
                    }
                    else if ("-p".equals(arg)) {
                      i++;
                      if (i < this.strings.length) {
                        String nextArg = this.strings[i];
                        if (isOption(nextArg)) {
                          printHelp();
                          return false;
                        }
                        File file = new File(nextArg);
                        if (file.exists()) {
                          SmartPreferences preferences = 
                            GCAndMemoryVisualizerImplPreferenceInitalizer.getInstance().getPreferences();
                          try {
                            preferences.importPreferences(file);
                          } catch (Exception e) {
                            TRACE.warning(e.toString());
                            return false;
                          }
                        }
                        else {
                          String name = nextArg + ".epf";
                          InputStream stream = TemplateList.class
                            .getResourceAsStream(name);
              
                          if (stream == null) {
                            TRACE.warning(MessageFormat.format(
                              PREFERENCES_FILE_DOES_NOT_EXIST, 
                              new Object[] { file.getAbsolutePath() }));
                          } else {
                            SmartPreferences preferences = 
                              GCAndMemoryVisualizerImplPreferenceInitalizer.getInstance().getPreferences();
                            try {
                              preferences.importPreferences(stream);
                            } catch (Exception e) {
                              TRACE.warning(e.toString());
                              return false;
                            }
                          }
                        }
                      } else {
                        printHelp();
                        return false;
                      }
                    } else if ("-display".equals(arg)) {
                      i++;
                      boolean dimensionsOK = false;
                      if (i < this.strings.length) {
                        String nextArg = this.strings[i];
                        String[] dimensions = nextArg.split("x");
                        try {
                          int width = Integer.valueOf(dimensions[0]).intValue();
                          int height = Integer.valueOf(dimensions[1]).intValue();
                          if ((width > 0) && (height > 0)) {
                            setImageSize(width, height);
                            dimensionsOK = true;
                          }
                        } catch (Exception localException1) {
                        }
                      }
                      if (!dimensionsOK)
                      {
                        printHelp();
                        return false;
                      }
                    }
                  }
              
                  if (!hitFileArguments) {
                    handleNoFileArguments();
                  }
              
                  return true;
                }
              
                protected abstract void setOutputFolder(String paramString);
              
                protected void setImageSize(int width, int height)
                {
                }
              
                protected void handleNoFileArguments()
                {
                }
              
                protected abstract void addFile(File paramFile)
                  throws GCAndMemoryVisualizerParsedDataCorruptedException;
              
                protected void addDirectory(File directory)
                {
                  File[] files = directory.listFiles();
                  for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    try {
                      if (!file.isDirectory())
                        addFile(file);
                    }
                    catch (GCAndMemoryVisualizerParsedDataCorruptedException e) {
                      TRACE.warning(MessageFormat.format(
                        Messages.getString("ArgumentHandler.34"), new Object[] { 
                        file.getAbsolutePath(), e }));
                    }
                  }
                }
              
                private boolean isOption(String nextArg) {
                  return (nextArg != null) && (nextArg.startsWith("-"));
                }
              
                protected String getAdditionalHelp() {
                  return "";
                }
              
                private void printHelp() {
                  TRACE.info(HELP_TEXT + getAdditionalHelp() + EXAMPLE_TEXT);
                }
              
                public boolean setReturnCodeOnError() {
                  return this.setReturnCodeOnError;
                }
              }

